package com.ajview.system.service.impl;

import com.ajview.common.core.domain.eneity.SysMenu;
import com.ajview.common.utils.StringUtils;
import com.ajview.system.mapper.SysMenuMapper;
import com.ajview.system.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author ZhangJunJie
 * @Date 2022-04-26
 * <p>
 * 概要：
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Resource
    private SysMenuMapper sysMenuMapper;

    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public Set<String> selectMenuListByUserId(Long userId) {
        // 查询该用户所有的菜单权限字符串
        List<String> perms = sysMenuMapper.selectMenuListByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms) {
            if (!StringUtils.isEmpty(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }
}
